package myframe.htmlutil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Adminlte {
	
	public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
		//adminlte("http://adminlte.la998.com/index.html");
		paPage("http://adminlte.la998.com/pages/calendar.html");
	}
	
	
	public static void adminlte(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
		//设置浏览器内核
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setTimeout(1000);
        webClient.getCookieManager().setCookiesEnabled(true);
        //设置Ajax控制器
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        //获取页面  
        HtmlPage page = webClient.getPage(url);//登陆页面
        
        DomNodeList<DomElement> aliList =  page.getElementsByTagName("a");
        for(DomElement dom:aliList){
        	try{
        		if(StringUtils.isNotEmpty(dom.getAttribute("href")) && dom.getAttribute("href").indexOf("http")<0
        				&&  dom.getAttribute("href").indexOf("logout")== -1
        				&& dom.getAttribute("href").contains(".html")){
		        		url =  dom.getAttribute("href").replace("/../", "/");
		        		if(!url.contains("adminlte")){
		        			url = "http://adminlte.la998.com/"+url;
		        		}
		        		
	        			paPage(url);
        		}
        		
        	}catch(Exception e){
        		
        	}
        }
        log.info("end..............");
        log.info("end..............");
        log.info("end..............");
        log.info("end..............");
	}
	
	public static void paPage(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
		
		//设置浏览器内核
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setTimeout(1000);
        webClient.getCookieManager().setCookiesEnabled(true);
        //设置Ajax控制器
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        //获取页面  
        HtmlPage page = webClient.getPage(url);//登陆页面
        downLoadJsCss(page);
        
        String content=page.asXml();
        
        url = url.replace("http://", "");
		url = url.replace("https://", "");
		url = "D:/"+url;
        int lastIndex = url.lastIndexOf("/");
        String path = url.substring(0,lastIndex);
        File pathFile = new File(path);
        if(!pathFile.exists()){
        	pathFile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(url);
        fos.write(content.getBytes());
        fos.close();
	}
	
	
	
	public static void downLoadJsCss(HtmlPage page){
		  DomNodeList<DomElement> scriptList =  page.getElementsByTagName("script");
	        for(DomElement dom:scriptList){
	        	try{
	        		String url = dom.getAttribute("src");
	        		if(url.startsWith("../")&&!url.contains("adminlte")){
	        			url = url.replace("../", "http://adminlte.la998.com/");
	        		}
	        		if(!url.contains("adminlte")){
	        			url = "http://adminlte.la998.com/"+url;
	        		}
	        		url = url.replace("/../", "/");
	        		if(StringUtils.isNotEmpty(url) && url.indexOf("http")>=0){
	        			String path = url.replace("http://", "");
	        			path = path.replace("https://", "");
	        			path = "D:/"+path;
	        			HtmlDo.downloadFile(url,path);
		        		log.info(url);
	        		}
	        	}catch(Exception e){
	        		
	        	}
	        	
	        }
	        
	        DomNodeList<DomElement> linkList =  page.getElementsByTagName("link");
	        for(DomElement dom:linkList){
	        	try{
	        		String url = dom.getAttribute("href");
	        		if(url.startsWith("../")&&!url.contains("adminlte")){
	        			url = url.replace("../", "http://adminlte.la998.com/");
	        		}
	        		if(!url.contains("adminlte")){
	        			url = "http://adminlte.la998.com/"+url;
	        		}
	        		url = url.replace("/../", "/");
	        		if(StringUtils.isNotEmpty(url) && url.indexOf("http")>=0){
	        			String path = url.replace("http://", "");
	        			path = path.replace("https://", "");
	        			path = "D:/"+path;
	        			HtmlDo.downloadFile(url,path);
		        		log.info(url);
	        		}
	        		
	        	}catch(Exception e){
	        		
	        	}
	        }
	        
	        
	        DomNodeList<DomElement> imgList =  page.getElementsByTagName("img");
	        for(DomElement dom:imgList){
	        	try{
	        		String url = dom.getAttribute("src");
	        		if(url.startsWith("../")&&!url.contains("adminlte")){
	        			url = url.replace("../", "http://adminlte.la998.com/");
	        		}
	        		if(!url.contains("adminlte")){
	        			url = "http://adminlte.la998.com/"+url;
	        		}
	        		url = url.replace("/../", "/");
	        		if(StringUtils.isNotEmpty(url) && url.indexOf("http")>=0){
	        			String path = url.replace("http://", "");
	        			path = path.replace("https://", "");
	        			path = "D:/"+path;
	        			HtmlDo.downloadFile(url,path);
		        		log.info(url);
	        		}
	        		
	        	}catch(Exception e){
	        		
	        	}
	        	
	        }
	        
	        
	        DomNodeList<DomElement> metaList =  page.getElementsByTagName("meta");
	        for(DomElement dom:metaList){
	        	try{
	        		String url = dom.getAttribute("href");
	        		if(url.startsWith("../")&&!url.contains("adminlte")){
	        			url = url.replace("../", "http://adminlte.la998.com/");
	        		}
	        		if(!url.contains("adminlte")){
	        			url = "http://adminlte.la998.com/"+url;
	        		}
	        		url = url.replace("/../", "/");
	        		if(StringUtils.isNotEmpty(url) && url.indexOf("http")>=0){
	        			String path = url.replace("http://", "");
	        			path = path.replace("https://", "");
	        			path = "D:/"+path;
	        			HtmlDo.downloadFile(url,path);
		        		log.info(url);
	        		}
	        		
	        	}catch(Exception e){
	        		
	        	}
	        	
	        }
	  }

}
